<?php

define("SALES_ORDER_DEFAULT_NUM_ITEMS","sales_order_default_num_items");	// variable key name
define("DEFAULT_NUM_ITEMS",3);

/**
 * form sales
 */
function sales_form(&$form_state){

	$form = array();
	
	$form["accts"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Accounts")
	);
	
	$form["accts"]["account"] = array(
		"#type"=>"textfield",
		"#title"=>t("Account"),
		"#description"=>t("Please select an account.")
	);
	
	$form["accts"]["account-add"] = array(
		"#type"=>"button",
		"#value"=>t("Split Account"),
		"#validate"=>array("sales_acct_add_validate"),
		"#submit"=>array("sales_acct_add_submit")
	);
	
	$form["accts"]["balance"] = array(
		"#type"=>"textfield",
		"#title"=>t("Balance"),
		"#description"=>t("Balance")
	);
	
	$form["details"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Details")
	);
	
	$form["details"]["ref"] = array(
		"#type"=>"textfield",
		"#title"=>t("Ref #"),
		"#description"=>t("Reference number")
	);
	
	$form["details"]["date"] = array(
		"#type"=>"date",
		"#title"=>t("Date"),
		"#description"=>t("Date")
	);
	
	$form["details"]["customer"] = array(
		"#type"=>"textfield",
		"#title"=>t("Customer"),
		"#description"=>t("Please select a customer")
	);
	
	$form["details"]["memo"] = array(
		"#type"=>"textfield",
		"#title"=>t("Memo"),
		"#description"=>t("Please enter a memo")
	);
	
	$form["invoices"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Invoices")
	);
	
	$form["invoices"]["item-check"] = array(
		"#type"=>"checkbox",
		"#title"=>t("")
	);
	
	$form["invoices"]["item-id"] = array(
		"#type"=>"textfield",
		"#title"=>t("Item Id"),
		"#description"=>t("Item id")
	);
	
	$form["items"]["item-add"] = array(
		"#type"=>"submit",
		"#value"=>t("Add")
	);
	
	$form["submit"] = array(
		"#type"=>"submit",
		"#value"=>t("Submit")
	);
	
	return $form;
}

/**
 * sales validation
 * 
 * @param form object $form
 * @param form state $form_state
 */
function sales_form_validate($form,&$form_state){
	
}

/**
 * sales submit
 * 
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
function sales_form_submit($form,&$form_state){
	
}

/**
 * sales invoice
 */
function sales_order_form($form_state){
	$form = array();
	
	$form["details"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Details")
	);
	
	$form["details"]["units-on-hand"] = array(
		"#type"=>"textfield",
		"#title"=>t("Units on Hand"),
		"#description"=>t("Units on hand")
	);
	
	$form["details"]["reordering-point"] = array(
		"#type"=>"textfield",
		"#title"=>t("Reordering Point"),
		"#description"=>t("Reordering point")
	);
	
	$form["details"]["so-number"] = array(
		"#type"=>"textfield",
		"#title"=>t("SO#"),
		"#description"=>t("Sales order number")
	);
	
	$form["details"]["date"] = array(
		"#type"=>"date",
		"#title"=>t("Date"),
		"#description"=>t("Date")
	);
	
	$form["details"]["customer"] = array(
		"#type"=>"textfield",
		"#title"=>t("Customer"),
		"#description"=>t("Please select a customer")
	);
	
	$form["details"]["address"] = array(
		"#type"=>"textarea",
		"#title"=>t("Address"),
		"#description"=>t("Please enter the customer's address")
	);
	
	$form["details"]["instructions"] = array(
		"#type"=>"textarea",
		"#title"=>t("Specific Instructions"),
		"#description"=>t("Specific instructions")
	);
	
	$form["details"]["sales-rep"] = array(
		"#type"=>"textfield",
		"#title"=>t("Sales Representative"),
		"#description"=>t("Name of sales representative")
	);
	
	$form["details"]["job"] = array(
		"#type"=>"textfield",
		"#title"=>t("Job"),
		"#description"=>t("Job")
	);
	
	$form["details"]["shipping-method"] = array(
		"#type"=>"textfield",
		"#title"=>t("Shipping Method"),
		"#description"=>t("Shipping method")
	);
	
	$form["details"]["shipping-terms"] = array(
		"#type"=>"textfield",
		"#title"=>t("Shipping Terms"),
		"#description"=>t("Shipping terms")
	);
	
	$form["details"]["date-delivery"] = array(
		"#type"=>"date",
		"#title"=>t("Delivery Date"),
		"#description"=>t("Delivery date")
	);
	
	$form["details"]["payment-terms"] = array(
		"#type"=>"textfield",
		"#title"=>t("Payment Terms"),
		"#description"=>t("Payment terms")
	);
	
	$form["details"]["location"] = array(
		"#type"=>"textfield",
		"#title"=>t("Location"),
		"#description"=>t("Location")
	);
	
	$form["items"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Items")
	);
	
	$num_items = variable_get(SALES_ORDER_DEFAULT_NUM_ITEMS,DEFAULT_NUM_ITEMS);
	for($i=0;$i<$num_items;$i++){
		$form["items"]["item-code-$i"] = array(
			"#type"=>"textfield",
			"#title"=>t("Item Code"),
			"#description"=>t("Item code")
		);
		
		$form["items"]["item-qty-$i"] = array(
			"#type"=>"textfield",
			"#title"=>t("Qty"),
			"#description"=>t("Quantity")
		);
		
		$form["items"]["item-desc-$i"] = array(
			"#type"=>"textfield",
			"#title"=>t("Description"),
			"#description"=>t("Item description")
		);
		
		$form["items"]["item-unit-price-$i"] = array(
			"#type"=>"textfield",
			"#title"=>t("Unit Price"),
			"#description"=>t("Unit price")
		);
		
		$form["items"]["item-total-$i"] = array(
			"#type"=>"textfield",
			"#title"=>t("Line Total"),
			"#description"=>t("Line total")
		);
	}
	
	$form["items"]["subtotal"] = array(
		"#type"=>"textfield",
		"#title"=>t("Subtotal"),
		"#description"=>t("Subtotal")
	);
	
	$form["items"]["vat"] = array(
		"#type"=>"textfield",
		"#title"=>t("VAT %12"),
		"#description"=>t("Value Added Tax")
	);
	
	$form["items"]["total"] = array(
		"#type"=>"textfield",
		"#title"=>t("Total"),
		"#description"=>t("Total")
	);
	
	$form["taxes"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Witholding Taxes")
	);
	
	$form["taxes"]["ewt"] = array(
		"#type"=>"textfield",
		"#title"=>t("EWT"),
		"#description"=>t("Expanded Witholding Tax")
	);
	
	$form["taxes"]["FWT"] = array(
		"#type"=>"textfield",
		"#title"=>t("FWT"),
		"#description"=>t("//TODO FWT")
	);
	
	$form["taxes"]["wgmp"] = array(
		"#type"=>"textfield",
		"#title"=>t("WGMP"),
		"#descriptipn"=>t("//TODO WGMP")
	);
	
	$form["submit"] = array(
		"#type"=>"submit",
		"#value"=>"Save"
	);
	
	return $form;
}

function sales_order_form_validate($form,&$form_state){
	
}

function sales_order_form_submit($form,&$form_state){
	
}

function theme_sales_order_form($form){
	$s = "<div id=\"sales-order-form\">";

	$titles = array(
		"sales-rep"=>$form["details"]["sales-rep"]["#title"],
		"job"=>$form["details"]["job"]["#title"],
		"shipping-method"=>$form["details"]["shipping-method"]["#title"],
		"shipping-terms"=>$form["details"]["shipping-terms"]["#title"],
		"date-delivery"=>$form["details"]["date-delivery"]["#title"],
		"payment-terms"=>$form["details"]["payment-terms"]["#title"],
		"location"=>$form["details"]["location"]["#title"]
	);
	
	$desc = array(
		"sales-rep"=>$form["details"]["sales-rep"]["#description"],
		"job"=>$form["details"]["job"]["#title"],
		"shipping-method"=>$form["details"]["shipping-method"]["#description"],
		"shipping-terms"=>$form["details"]["shipping-terms"]["#description"],
		"date-delivery"=>$form["details"]["date-delivery"]["#description"],
		"payment-terms"=>$form["details"]["payment-terms"]["#description"],
		"location"=>$form["details"]["location"]["#description"]
	);
	
	unset($form["details"]["sales-rep"]["#title"]);
	unset($form["details"]["job"]["#title"]);
	unset($form["details"]["shipping-method"]["#title"]);
	unset($form["details"]["shipping-terms"]["#title"]);
	unset($form["details"]["date-delivery"]["#title"]);
	unset($form["details"]["payment-terms"]["#title"]);
	unset($form["details"]["location"]["#title"]);
	
	unset($form["details"]["sales-rep"]["#description"]);
	unset($form["details"]["job"]["#description"]);
	unset($form["details"]["shipping-method"]["#description"]);
	unset($form["details"]["shipping-terms"]["#description"]);
	unset($form["details"]["date-delivery"]["#description"]);
	unset($form["details"]["payment-terms"]["#description"]);
	unset($form["details"]["location"]["#description"]);
	
	$rows[] = array(
		array(
			"data"=>drupal_render($form["details"]["sales-rep"]),
			"title"=>$desc["sales-rep"]
		),
		array(
			"data"=>drupal_render($form["details"]["job"]),
			"title"=>$desc["job"]
		),
		array(
			"data"=>drupal_render($form["details"]["shipping-method"]),
			"title"=>$desc["shipping-method"]
		),
		array(
			"data"=>drupal_render($form["details"]["shipping-terms"]),
			"title"=>$desc["shipping-terms"]
		),
		array(
			"data"=>drupal_render($form["details"]["date-delivery"]),
			"title"=>$desc["date-delivery"]
		),
		array(
			"data"=>drupal_render($form["details"]["payment-terms"]),
			"title"=>$desc["payment-terms"]
		),
		array(
			"data"=>drupal_render($form["details"]["location"]),
			"title"=>$desc["location"]
		)
	);
	
	$s .= "\n\t<div id=\"details\">";
	$s .= theme("table",$titles,$rows,array(),t("Details"));
	$s .= "\n\t</div>";
	
	$titles = array(
		$form["items"]["item-code-0"]["#title"],
		$form["items"]["item-qty-0"]["#title"],
		$form["items"]["item-desc-0"]["#title"],
		$form["items"]["item-unit-price-0"]["#title"],
		$form["items"]["item-total-0"]["#title"]
	);
	
	$desc = array(
		"item-code"=>$form["items"]["item-code-0"]["#description"],
		"item-qty"=>$form["items"]["item-qty-0"]["#description"],
		"item-desc"=>$form["items"]["item-desc-0"]["#description"],
		"item-unit-price"=>$form["items"]["item-unit-price-0"]["#description"],
		"item-total"=>$form["items"]["item-total-0"]["#description"]
	);
	
	$keys = element_children($form["items"]);
	foreach($keys as $key){
		unset($form["items"]["$key"]["#title"]);
		unset($form["items"]["$key"]["#description"]);
	}
	
	$num_items = variable_get(SALES_ORDER_DEFAULT_NUM_ITEMS,DEFAULT_NUM_ITEMS);
	$items = array();
	for($i=0;$i<$num_items;$i++){
		$items[] = array(
			array(
				"data"=>drupal_render($form["items"]["item-code-$i"]),
				"title"=>$desc["item-code"]
			),
			array(
				"data"=>drupal_render($form["items"]["item-qty-$i"]),
				"title"=>$desc["item-qty"]
			),
			array(
				"data"=>drupal_render($form["items"]["item-desc-$i"]),
				"title"=>$desc["item-desc"]
			),
			array(
				"data"=>drupal_render($form["items"]["item-unit-price-$i"]),
				"title"=>$desc["item-unit-price"]
			),
			array(
				"data"=>drupal_render($form["items"]["item-total-$i"]),
				"title"=>$desc["item-total"]
			)
		);		
	}
	
	$s .= "\n\t<div id=\"items\">";
	$s .= theme("table",$titles,$items,array(),t("Items"));
	$s .= "\n\t</div>";
	
	$s .= drupal_render($form);
	
	$s .= "\n</div>";
	
	return $s;
}

function sales_rep_form(){
	$form = array();
	
	$form["details"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Details")
	);
	
	$form["details"]["username"] = array(
		"#type"=>"textfield",
		"#title"=>t("User name"),
		"#description"=>t("Please select a user name"),
		"#autocomplete_path"=>"user/autocomplete",
		"#required"=>TRUE
	);
	
	$form["details"]["fullname"] = array(
		"#type"=>"textfield",
		"#title"=>t("Full Name"),
		"#description"=>t("Please enter user's full name"),
		"#required"=>TRUE
	);
	
	$form["submit"] = array(
		"#type"=>"submit",
		"#value"=>t("Save")
	);
	
	return $form;
}

function sales_rep_form_validate($form,&$form_state){
	$name = $form_state["values"]["username"];
	$user = user_load(array("name"=>$name));
	if($user === FALSE){
		form_set_error("username",t("Please select an existing user."));
	}
	
	if($form_state["values"]["fullname"] == ""){
		form_set_error("fullname",t("Please enter user's full name."));
	}
}

function sales_rep_form_submit($form,&$form_state){
	_preload_sales();
	
	$name = $form_state["values"]["username"];
	$full = $form_state["values"]["fullname"];
	
	$user = user_load(array("name"=>$name));
	if($user){
		Sales::add_sales_representative($user->uid,$full);
		
		drupal_set_message(t("Sales representative added."));
	} else {
		watchdog("sales","Unable to add sales representative. Unable to load user '%name'.",
			array("%name"=>$name),WATCHDOG_ERROR);
		drupal_set_message(t("Unable to add sales representative."),"error");
	}
}

function sales_item_form(){
	$form = array();
	
	$form["item"] = array(
		"#type"=>"fieldset",
		"#title"=>t("Item")
	);
	
	$form["item"]["item-id"] = array(
		"#type"=>"textfield",
		"#title"=>t("Id"),
		"#description"=>t("Item identifier"),
		"#attributes"=>array("readonly"=>"readonly")
	);
	
	$form["item"]["item-desc"] = array(
		"#type"=>"textfield",
		"#title"=>t("Description"),
		"#description"=>t("Item description")
	);
	
	return $form;
}

function sales_invoices_form(){
	$form = array();
	
	
	
	return $form;
}

/**
 * callback for sales/representative/autocomplete/%
 * 
 * @param unknown_type $name
 */
function sales_rep_autocomplete($name){
	_preload_sales();

	$ret = array();
	if($name != ""){
		$reps = Sales::get_sales_representatives($name);
		foreach($reps as $r){
			// TODO sales_rep_autocomplete
		}
	}
	
	drupal_json($ret);
}

function sales_item_autocomplete($description){
	_preload_sales();
	
	$ret = array();
	if($description != ""){
		// TODO get items
	}
	
	drupal_json($ret);
}